package com.foreknow.service.impl;

import com.foreknow.dao.HelloDao;
import com.foreknow.dao.inpl.HelloDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Hello;
import com.foreknow.service.HelloService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class HelloServiceImpl implements HelloService {
    //1. 链接数据库
    DBManager dbManager= DBManager.getInstance();
    Connection conn =dbManager.getConnection();
    HelloDaoImpl helloDao = new HelloDaoImpl();

    /**
     * 查询所有
     * @param hello
     * @return
     */
    @Override
    public List<Object> add() {
         //调用BaseDao中的Connection(conn)方法
        helloDao.setConnection(conn);
       List<Object> list= helloDao.add();

        return list;
    }

    @Override
    public Hello query(int id) {
        //调用BaseDao中的Connection(conn)方法
        helloDao.setConnection(conn);
       Hello hello =helloDao.query(id);
        return hello;
    }

    @Override
    public boolean update(Hello hello) {
        //调用BaseDao中的Connection(conn)方法
        helloDao.setConnection(conn);

        Boolean update=null;
        try {
            update= helloDao.update(hello);
            //手动提交事务提交事务
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return update;
    }
    }

